<template>
	<view>

		<u-popup :show="show" @close="close" @open="open" mode="center" bgColor="transparent">
			<view class="popup-box">
				<view style="width: 620rpx;height: 800rpx;">
					<view class="popup-top">
						<view class="popup-title">选择战机</view>
						<view class="close-btn" @click="close()">
							<image src="/static/icon-images/close.png" mode="widthFix"></image>
						</view>
					</view>
					<scroll-view class="scroll" :scroll-y="true">
						<view class="box">
							<view class="card list" v-for="(item,index) in plantList" :key="index"
								@click="checkPlant(item)">
								<view class="logo">
									<image class="logo" :src="getPlane(item.airLevel)" mode="heightFix"></image>
								</view>
								<view class="text">
									{{item.airLevel}}级战机
								</view>
							</view>
						</view>
						<view v-if="plantList.length==0" class="none">暂无战机</view>
					</scroll-view>
				</view>
			</view>
		</u-popup>

	</view>
</template>

<script>
	import {
		getPlane
	} from '@/api/plane.js'
	export default {
		props: {
			show: {
				type: Boolean,
				default: false
			},
			checkPlantList: {
				type: Array,
				default: () => []
			},
			checkPlant1: {
				type: Object,
				default: () => {}
			},
			checkPlant2: {
				type: Object,
				default: () => {}
			},
			checkSquare: {
				type: Number,
				default: 0
			}
		},
		data() {
			return {
				plantList: []
			};
		},
		methods: {
			getPlane(index) {
				switch (index) {
					case 1:
						return '/static/fly-images/1.png'
					case 2:
						return '/static/fly-images/2.png'
					case 3:
						return '/static/fly-images/3.png'
					case 4:
						return '/static/fly-images/4.png'
					case 5:
						return '/static/fly-images/5.png'
					case 6:
						return '/static/fly-images/6.png'
					case 7:
						return '/static/fly-images/7.png'
					case 8:
						return '/static/fly-images/8.png'
					case 9:
						return '/static/fly-images/9.png'
					case 10:
						return '/static/fly-images/10.png'
					default:
						break;
				}
			},
			open() {
				this.plantList = []
				this.$emit('update:show', true);
				this.getList()
			},
			close() {
				this.$emit('update:show', false);
			},
			checkPlant(item) {
				if (this.checkSquare == 1) {
					this.$emit('update:checkPlant1', item);
					this.$emit('update:show', false);
					this.$set(this.checkPlantList, 0, {
						...this.checkPlantList[0],
						airLevel: item.airLevel
					})
				} else if (this.checkSquare == 2) {
					this.$emit('update:checkPlant2', item);
					this.$emit('update:show', false);
					this.$set(this.checkPlantList, 1, {
						...this.checkPlantList[1],
						airLevel: item.airLevel
					})
				} else {
					console.log('未选择飞机');
				}

				console.log(this.checkPlantList);
			},
			async getList() {
				const res = await getPlane();
				this.plantList = []; // 清空，准备重新填充

				for (let i = 0; i < res.data.length; i++) {
					for (let j = 0; j < res.data[i].list.length; j++) {
						const plane = res.data[i].list[j];
						// 如果当前飞机是 checkPlant1 或 checkPlant2，跳过（不 push）
						if (
							(this.checkPlant1 && this.checkPlant1.uuid === plane.uuid) ||
							(this.checkPlant2 && this.checkPlant2.uuid === plane.uuid)
						) {
							continue; //跳过这一项，继续下一个
						}
						this.plantList.push(plane);
					}
				}
			}
		}
	}
</script>

<style lang="scss">
	.popup-box {
		background: linear-gradient(135deg, #0a1a3a 0%, #050a1a 100%);
		border: 1rpx solid rgba(255, 255, 255, 0.15);
		border-radius: 20rpx;
		overflow-y: auto;
		padding: 30rpx;
	}

	.popup-top {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.popup-title {
		font-weight: 700;
		font-size: 38rpx;
		color: #e2e8f0;
	}

	.close-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 60rpx;
		height: 60rpx;
		border-radius: 50%;
		background: rgba(255, 255, 255, 0.15);

		image {
			display: block;
			width: 28rpx;
			height: 28rpx;
		}
	}

	.scroll {
		width: 100%;
		height: 720rpx;

		.box {
			padding: 30rpx;
			display: flex;
			flex-wrap: wrap;
			gap: 20rpx;
		}

		.list {
			padding: 20rpx;
		}

		.logo {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 220rpx;
			height: 220rpx;
		}

		.text {
			margin-top: 20rpx;
			font-size: 28rpx;
			color: #e2e8f0;
			line-height: 1;
			font-weight: 500;
		}

		.none {
			text-align: center;
			margin-top: 20rpx;
			font-size: 28rpx;
			color: #f6f6f6;
			line-height: 1;
			font-weight: 500;
		}
	}
</style>